package org.jeecg.modules.assets.controller.app;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.assets.dto.*;
import org.jeecg.modules.assets.entity.AssetsDoubleQuotationResult;
import org.jeecg.modules.assets.entity.AssetsRandomStock;
import org.jeecg.modules.assets.service.IAssetsDoubleQuotationResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 复盘管理
 * @Author: jeecg-boot
 * @Date: 2023-12-20
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "移动端复盘管理")
@RestController
@RequestMapping("/app/assets/assetsDoubleQuotationResult")
public class AppAssetsDoubleQuotationResultController extends JeecgController<AssetsDoubleQuotationResult, IAssetsDoubleQuotationResultService> {
    @Autowired
    private IAssetsDoubleQuotationResultService assetsDoubleQuotationResultService;

    /**
     * 分页列表查询
     *
     * @param assetsDoubleQuotationResult
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "复盘管理-分页列表查询")
    @ApiOperation(value = "复盘管理-分页列表查询", notes = "复盘管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AssetsDoubleQuotationResult assetsDoubleQuotationResult,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(name = "queryType", required = false, defaultValue = "1") String queryType,
                                   HttpServletRequest req) {
        QueryWrapper<AssetsDoubleQuotationResult> queryWrapper = QueryGenerator.initQueryWrapper(assetsDoubleQuotationResult, req.getParameterMap());
        Page<AssetsDoubleQuotationResult> page = new Page<AssetsDoubleQuotationResult>(pageNo, pageSize);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //只获取该用户当前部门的列表
        queryWrapper.eq("dept_id", loginUser.getDepartIds());
        //按照创建时间倒序
        queryWrapper.orderByDesc("create_time");
        IPage<AssetsDoubleQuotationResult> pageList = assetsDoubleQuotationResultService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param assetsDoubleQuotationResult
     * @return
     */
    @AutoLog(value = "复盘管理-添加")
    @ApiOperation(value = "复盘管理-添加", notes = "复盘管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AssetsDoubleQuotationResult assetsDoubleQuotationResult) {
        assetsDoubleQuotationResultService.save(assetsDoubleQuotationResult);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param assetsDoubleQuotationResult
     * @return
     */
    @AutoLog(value = "复盘管理-编辑")
    @ApiOperation(value = "复盘管理-编辑", notes = "复盘管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<?> edit(@RequestBody AssetsDoubleQuotationResult assetsDoubleQuotationResult) {
        assetsDoubleQuotationResultService.updateById(assetsDoubleQuotationResult);
        return Result.OK("编辑成功!");
    }


    @AutoLog(value = "复盘管理-创建")
    @ApiOperation(value = "复盘管理-创建", notes = "复盘管理-创建")
    @PostMapping(value = "/startAssets")
    public Result<?> startAssets(@RequestBody StartAssetsDoubleQuotaionDTO startAssets) {
        String endTime = startAssets.getEndTime();
        DateTime endDate = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss");
        //当前时间加一天
        Calendar calendar = DateUtil.date(new Date()).toCalendar();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        Date time = calendar.getTime();
        if(!endDate.isAfter(time))  {
            return Result.error("结束时间大于当前时间24小时");

      }

        return  assetsDoubleQuotationResultService.startAssets(startAssets);

    }

    @AutoLog(value = "复盘管理-获取用户初盘信息")
    @ApiOperation(value = "复盘管理-获取用户初盘信息", notes = "复盘管理-获取用户初盘信息")
    @GetMapping(value = "/getUsetAssetsInfo")
    public Result<AssetsListDoubleDTO> getUsetAssetsInfo(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "billNo") String billNo,
            @RequestParam(name = "queryType") Integer queryType,
            @RequestParam(name = "owner") String owner,
            @RequestParam(name = "inventoryStatus",required = false) String inventoryStatus,
            @RequestParam(name = "assetName",required = false) String assetName,
            @RequestParam(name = "asset", required = false) String asset,
            @RequestParam(name = "preliminaryStatus",required = false) Integer preliminaryStatus,
            @RequestParam(name = "company",required = false) String company
            ) {
        LambdaQueryWrapper<AssetsDoubleQuotationResult> lambda = new QueryWrapper<AssetsDoubleQuotationResult>().lambda();
        lambda.eq(AssetsDoubleQuotationResult::getBillNo, billNo);
        AssetsDoubleQuotationResult doubleQuotationResult = assetsDoubleQuotationResultService.getOne(lambda);
        Page page = new Page<>(pageNo, pageSize);
        AssetsListDoubleDTO usetAssetsInfo = assetsDoubleQuotationResultService.getUsetAssetsInfo(doubleQuotationResult, page, queryType,owner,inventoryStatus,preliminaryStatus,asset,company);

        return  Result.OK(usetAssetsInfo);
    }
    @AutoLog(value = "复盘结果管理-操作复盘明细")
    @ApiOperation(value = "复盘结果管理-操作复盘明细", notes = "复盘结果管理-操作复盘明细")
    @PostMapping(value = "/optionAssetsItem")
    public Result optionAssetsItem(@RequestBody OptionPreliminaryAssetsItemDTO optionPreliminaryAssetsItem) {
        try{  assetsDoubleQuotationResultService.optionAssetsItem(optionPreliminaryAssetsItem);
        }catch (Exception e){
            log.error("操作复盘明细失败",e);
            return Result.error("操作复盘明细失败:"+e.getMessage());
        }
        return Result.OK();
    }
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "复盘管理-通过id删除")
    @ApiOperation(value = "复盘管理-通过id删除", notes = "复盘管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        assetsDoubleQuotationResultService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "复盘管理-批量删除")
    @ApiOperation(value = "复盘管理-批量删除", notes = "复盘管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.assetsDoubleQuotationResultService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "复盘管理-通过id查询")
    @ApiOperation(value = "复盘管理-通过id查询", notes = "复盘管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AssetsDoubleQuotationResult assetsDoubleQuotationResult = assetsDoubleQuotationResultService.getById(id);
        return Result.OK(assetsDoubleQuotationResult);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param assetsDoubleQuotationResult
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AssetsDoubleQuotationResult assetsDoubleQuotationResult) {
        return super.exportXls(request, assetsDoubleQuotationResult, AssetsDoubleQuotationResult.class, "复盘管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AssetsDoubleQuotationResult.class);
    }

}
